package com.coffee.admin.controller.supplier;

import com.coffee.admin.api.common.service.RedisService;
import com.coffee.admin.api.login.request.LoginByPasswordRequert;
import com.coffee.admin.api.login.request.LoginBySmsRequert;
import com.coffee.admin.api.platform.service.PlatformLoginService;
import com.coffee.admin.api.supplier.req.SupplierForgetPasswordReq;
import com.coffee.admin.api.supplier.service.SupplierLoginService;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.entity.PassToken;
import com.coffee.core.entity.SiteContextThreadLocal;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.IpUtils;
import com.coffee.core.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/supplier")
public class SupplierLoginController extends BaseController {

    @DubboReference
    private PlatformLoginService platformLoginService;
    @DubboReference
    private RedisService redisService;

    @PostMapping(value = "/passwordLogin")
    @OperLog(operModul = "登录", operDesc = "密码登录")
    public Result<String> passwordLogin(@RequestBody LoginByPasswordRequert req, HttpServletRequest request, HttpServletResponse response) {
        req.setLoginIp(IpUtils.getIp(request));
        req.setUserAgent(WebUtil.getUserAgent(request));
        String token = null;
        try {
            token = platformLoginService.passwordLogin(req);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        WebUtil.addCookie(response, "stoken", token);
        return Result.success(token);
    }

    @OperLog(operModul = "登录", operDesc = "验证码登录")
    @PostMapping(value = "/smsLogin")
    public Result<String> smsLogin(@RequestBody LoginBySmsRequert req, HttpServletRequest request, HttpServletResponse response) {
        req.setLoginIp(IpUtils.getIp(request));
        req.setUserAgent(WebUtil.getUserAgent(request));
        String token = null;
        try {
            token = platformLoginService.smsLogin(req);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        WebUtil.addCookie(response, "stoken", token);
        return Result.success(token);
    }

    @OperLog(operModul = "登录", operDesc = "忘记密码登录")
    @PostMapping(value = "/forget/password")
    public Result<String> forgetPassword(@RequestBody SupplierForgetPasswordReq req, HttpServletRequest request, HttpServletResponse response) {
        String token = null;
        try {
            token = platformLoginService.forgetPassword(req);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        WebUtil.addCookie(response, "stoken", token);
        return Result.success(token);
    }

    /***
     * @Description: 退出登录
     */
    @OperLog(operModul = "登录", operDesc = "退出登录")
    @GetMapping(value = "/logout")
    public Result<Void> logout(HttpServletResponse response) {
        PassToken passToken = SiteContextThreadLocal.get();
        String ptoken = passToken.getStoken();
        if (StringUtils.isNotEmpty(ptoken)) {
            WebUtil.deleteCookie(response, "stoken");
            passToken.setStoken(null);
            SiteContextThreadLocal.set(passToken);
            redisService.del(ptoken);
        }
        return Result.success(null);
    }
}
